/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.db;

import com.ttt.nor2.util.ExceptionUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Hashtable;
import org.apache.log4j.Logger;

/**
 *
 * @author anuphabm
 */
public class Smstrans {

    private Logger logger = Logger.getLogger(Smstrans.class);
    private String transid;
    private String ani;
    private String msgout;
    private String lang;
    private String type;
    private String sender;
    private String username;
    private String password;
    private String script;
    private String urlhttp;
    private int porthttp;
    private String urlproxy;
    private int portproxy;
    private Connection conn;
    private int time;
    private String result;
    private int statuscode;

    public int getStatuscode() {
        return statuscode;
    }

    public void setStatuscode(int statuscode) {
        this.statuscode = statuscode;
    }

    public void udSmst() {
        try {
            String sql = "update smstrans set status='Y', resend_count = resend_count+1, sendtime=now(), " +
                    "result=?, statuscode=?, ondate=date_format(now(),'%Y%m%d'),duration=? where transid = ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, result);
            pstmt.setInt(2, statuscode);
            pstmt.setInt(3, time);
            pstmt.setString(4, transid);

            int rec = pstmt.executeUpdate();
            logger.info("[" + transid + "]Update status Y " + rec + " recs.");
            pstmt.close();
            pstmt = null;
        } catch (Exception e) {
            logger.error("Exception update error! " + ExceptionUtil.getStackTrace(e));
        }
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public void setLockRec() {

        try {
            String sql = "update smstrans set status='S' where transid = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, transid);
            int rec = pstmt.executeUpdate();
            logger.info("[" + transid + "]Lock record " + rec + " recs.");
            pstmt.close();
            pstmt = null;
        } catch (Exception e) {
            logger.error("Exception lock record error! " + ExceptionUtil.getStackTrace(e));
        }
    }

    public String getPassword() {
        return password;
    }

    public int getPorthttp() {
        return porthttp;
    }

    public int getPortproxy() {
        return portproxy;
    }

    public String getScript() {
        return script;
    }

    public String getUrlhttp() {
        return urlhttp;
    }

    public String getUrlproxy() {
        return urlproxy;
    }

    public String getUsername() {
        return username;
    }

    public String getTransid() {
        return transid;
    }

    public void setTransid(String transid) {
        this.transid = transid;
    }

    public String getAni() {
        return ani;
    }

    public void setAni(String ani) {
        this.ani = ani;
    }

    public void setBulkid(Hashtable ht, int bulkid) {
        logger.debug("bulkid[" + bulkid + "]");
        Bulksms bsms = (Bulksms) ht.get(Integer.toString(bulkid));
        this.urlhttp = bsms.getUrl();
        this.porthttp = bsms.getPort();
        this.script = bsms.getScript();
        this.username = bsms.getUsername();
        this.password = bsms.getPassword();
        if (bsms.isProxy()) {
            this.urlproxy = bsms.getUrlproxy();
            this.portproxy = bsms.getPortproxy();
        }

    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public String getMsgout() {
        return msgout;
    }

    public void setMsgout(String msgout) {
        this.msgout = msgout;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(Hashtable ht, int sendid) {
        this.sender = (String) ht.get(Integer.toString(sendid));

    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
